<?php
/**
 * Created by phpstorm.
 * User: WML
 * Date: 2019/8/7 0007
 * Time: 14:58
 */
namespace app\index\controller;
use app\index\model\Users;
use think\Controller;
use think\facade\Session;
class Chat extends Controller
{
    /**
     * 用户列表
     * @return mixed
     */
    public function userList(){
        $user_id = session::get('user_id');
        $user_data = Users::all();
        $group_data = db("group")->select();
        $user = Users::get(session::get('user_id'));
        $this->assign([
                'user_data' => $user_data,
                'user_id' => $user_id,
                'group_data' => $group_data,
                'user' => $user
            ]);
        return $this->fetch();
    }
    /**
     * 聊天页面
     * @return mixed
     */
    public function communication(){
        $receive_user_id = $_GET['dialogue'];
        $send_user_data = Users::get(session::get('user_id'));
        $receive_user_data = Users::get($receive_user_id);
        $this->assign([
            'send' => $send_user_data,
            'receive' => $receive_user_data,
            'time' => time()
//            'no_read_data'=>$no_read_data
        ]);
        return $this->fetch();
    }

    /**
     * 保存聊天数据
     */
    public function saveData(){
        $data = request()->param();
        $row = [
            'send_id' => $data['send_id'],
            'receive_id' => $data['receive_id'],
            'content' => $data['data'],
            'is_read' => $data['is_read']
        ];
        $res = db("chat_content")->insert($row);
        if($res){
            return 200;
        }else{
            return 400;
        }
    }

    /**
     * 初始化聊天记录
     *
     */
    public function chatRecord(){
        $data = request()->param();
        $data = db("chat_content")
                ->where("(send_id=:send_id and receive_id=:receive_id) || (send_id=:receive_id1 and receive_id=:send_id1)")
                ->bind(['send_id' => $data['send_id'],'receive_id' => $data['receive_id'],'receive_id1' => $data['receive_id'],'send_id1' => $data['send_id']])
                ->limit(15)
                ->select();
        return $data;
    }

}